package com.foreveredu.erlms.service.impl;

import java.util.List;
import org.springframework.transaction.annotation.Transactional;

import com.foreveredu.erlms.domain.Enroll;
import com.foreveredu.erlms.domain.Examinee;
import com.foreveredu.erlms.domain.Grade;
import com.foreveredu.erlms.domain.MaxSNRecord;
import com.foreveredu.erlms.domain.PageModel;
import com.foreveredu.erlms.domain.Parameter;
import com.foreveredu.erlms.domain.Rank;

import com.foreveredu.erlms.persist.IEnrollDao;
import com.foreveredu.erlms.persist.IExamineeDao;
import com.foreveredu.erlms.persist.IGradeDao;
import com.foreveredu.erlms.persist.IMaxSNRecordDao;
import com.foreveredu.erlms.persist.IParameterDao;
import com.foreveredu.erlms.persist.IRankDao;
import com.foreveredu.erlms.service.IExamService;

@Transactional
public class ExamService implements IExamService {

	private IEnrollDao enrollDao = null;
	private IExamineeDao examineeDao = null;
	private IGradeDao gradeDao = null;
	private IRankDao rankDao = null;
	private IParameterDao parameterDao = null;
	private IMaxSNRecordDao maxSNDao = null;

	public IEnrollDao getEnrollDao() {
		return enrollDao;
	}

	public void setEnrollDao(IEnrollDao enrollDao) {
		this.enrollDao = enrollDao;
	}

	public IExamineeDao getExamineeDao() {
		return examineeDao;
	}

	public void setExamineeDao(IExamineeDao examineeDao) {
		this.examineeDao = examineeDao;
	}

	public IGradeDao getGradeDao() {
		return gradeDao;
	}

	public void setGradeDao(IGradeDao gradeDao) {
		this.gradeDao = gradeDao;
	}

	public IRankDao getRankDao() {
		return rankDao;
	}

	public void setRankDao(IRankDao rankDao) {
		this.rankDao = rankDao;
	}

	public IParameterDao getParameterDao() {
		return parameterDao;
	}

	public void setParameterDao(IParameterDao parameterDao) {
		this.parameterDao = parameterDao;
	}

	public void setMaxSNDao(IMaxSNRecordDao maxSNDao) {
		this.maxSNDao = maxSNDao;
	}

	public IMaxSNRecordDao getMaxSNDao() {
		return maxSNDao;
	}

	/*
	 * Enroll对象的事务处理
	 */
	@Override
	public void saveEnroll(Enroll enroll) {
		enrollDao.save(enroll);
	}

	@Override
	public void deleteEnrollById(int id) {
		Enroll enroll = enrollDao.findById(id);
		enrollDao.delete(enroll);
	}

	@Override
	public void updateEnroll(Enroll enroll) {
		enrollDao.update(enroll);
	}

	@Override
	public List<Enroll> findEnrolls() {
		return enrollDao.findAll();
	}

	@Override
	public Enroll findEnrollById(int id) {
		return enrollDao.findEnrollById(id);
	}

	public String getMaxEnrollNumber(String schoolCode, String rankCode,
			int examOrder) {
		String maxEnrollNumber = "";
		Examinee examinee = examineeDao.getExamineeOfMaxEnrollNumber(examOrder);
		if (examinee != null) {
			maxEnrollNumber = schoolCode
					+ rankCode
					+ (String.valueOf(Long
							.parseLong(examinee.getEnrollNumber()) + 1))
							.substring(8);
		} else {
			maxEnrollNumber = schoolCode + rankCode + "000001";
		}

		return maxEnrollNumber;
	}

	public String getMaxSerialNumber(String schoolCode, int examOrder) {
		String maxSerialNumber = "";
		Examinee examinee = examineeDao.getExamineeOfMaxSerialNumber(
				schoolCode, examOrder);
		if (examinee != null) {
			maxSerialNumber = String.valueOf(Long.parseLong(examinee
					.getSerialNumber()) + 1);
		} else {
			maxSerialNumber = schoolCode + "000001";
		}

		return maxSerialNumber;
	}

	@Override
	public PageModel findEnrollList(int offset, int examOrder) {
		return enrollDao.findEnrollList(offset, examOrder);
	}

	@Override
	public Enroll findEnrollByIdCardAndEnrollNumber(String idCard,
			String enrollNumber) {
		return enrollDao
				.findEnrollByIdCardAndEnrollNumber(idCard, enrollNumber);
	}

	@Override
	public List<Enroll> findEnrollsByExamOrder(int examOrder) {
		return enrollDao.findEnrollsByExamOrder(examOrder);
	}

	// 获得本届考试中按班级排序的未交费学生报名信息，以便导出DBF格式的报名数据
	public List<Enroll> findNoPaidEnrollsByExamOrder(int examOrder) {
		return enrollDao.findNoPaidEnrollsByExamOrder(examOrder);
	}

	@Override
	public PageModel findEnrollListByPaid(int offset, int examOrder) {
		return enrollDao.findEnrollListByPaid(offset, examOrder);
	}

	@Override
	public PageModel findEnrollListByIcon(int offset, int examOrder) {
		return enrollDao.findEnrollListByIcon(offset, examOrder);
	}

	@Override
	public List<Enroll> findEnrollByExamineeId(int id) {
		return enrollDao.findEnrollByExamineeId(id);
	}

	@Override
	public List<Enroll> findEnrollByIdCard(String idCard, int examOrder) {
		return enrollDao.findEnrollByIdCard(idCard, examOrder);
	}

	@Override
	public int findEnrollAllNumber(int examOrder) {
		return enrollDao.findEnrollAllNumber(examOrder);
	}

	@Override
	public int findEnrollPaidNumber(int examOrder) {
		return enrollDao.findEnrollPaidNumber(examOrder);
	}

	@Override
	public int findEnrollKeepGradeNumber(int examOrder) {
		return enrollDao.findEnrollKeepGradeNumber(examOrder);
	}

	@Override
	public int findEnrollPaidNumberByToday(String now) {
		return enrollDao.findEnrollPaidNumberByToday(now);
	}

	@Override
	public int findEnrollPaidNumberByTodayAndKeepGrade(String now) {
		return enrollDao.findEnrollPaidNumberByTodayAndKeepGrade(now);
	}

	@Override
	public Boolean findEnrollByDeptAndIdCard(int examOrder, String idCard,
			int rankId) {
		return enrollDao.findEnrollByDeptAndIdCard(examOrder, idCard, rankId);
	}

	@Override
	public Examinee findExamineeByEnrollNumber(String enrollNumber,
			int examOrderOfNow) {
		return examineeDao.findExamineeByEnrollNumber(enrollNumber,
				examOrderOfNow);
	}

	public Examinee findExamineeByIdCard(String idCard, int examOrder) {
		return examineeDao.findExamineeByIdCard(idCard, examOrder);
	}

	// @Override
	// public List<Enroll> findEnrollByIdCardAndSerialNumber(String idCard,
	// String serialNumber, int examOrder) {
	// return
	// enrollDao.findEnrollByIdCardAndSerialNumber(idCard,serialNumber,examOrder);
	// }

	/*
	 * Examinee对象的事务处理
	 */
	public void saveExaminee(Examinee examinee) {
		examineeDao.save(examinee);
	}

	public void deleteExamineeById(int id) {
		Examinee examinee = examineeDao.findById(id);
		examineeDao.delete(examinee);
	}

	public void updateExaminee(Examinee examinee) {
		examineeDao.update(examinee);
	}

	public List<Examinee> findExaminees() {
		return examineeDao.findAll();
	}

	public Examinee findExamineeById(int id) {
		return examineeDao.findById(id);
	}

	@Override
	public Examinee findExamineeBySerialNumber(String serialNumber,
			int examOrderOfNow) {
		return examineeDao.findExamineeBySerialNumber(serialNumber,
				examOrderOfNow);
	}

	@Override
	public Examinee findExamineeByTicketNumber(String ticketNumber,
			int examOrderOfNow) {
		return examineeDao.findExamineeByTicketNumber(ticketNumber,
				examOrderOfNow);
	}

	@Override
	public List<Integer> findExamOrderByExaminee() {
		return examineeDao.findExamOrderByExaminee();
	}

	/*
	 * Grade对象的事务处理
	 */
	public void saveGrade(Grade grade) {
		gradeDao.save(grade);
	}

	public void deleteGradeById(int id) {
		Grade grade = gradeDao.findById(id);
		gradeDao.delete(grade);
	}

	public void updateGrade(Grade grade) {
		gradeDao.update(grade);
	}

	public List<Grade> findGrades() {
		return gradeDao.findAll();
	}

	public Grade findGradeById(int id) {
		return gradeDao.findById(id);
	}

	@Override
	public Grade findGradeByIDCardAndTicketNumber(String idCard,
			String ticketNumber, int examOrder) {
		return gradeDao.findGradeByIDCardAndTicketNumber(idCard, ticketNumber,
				examOrder);
	}

	@Override
	public PageModel findGradeList(int offset, int examOrder) {
		return gradeDao.findGradeList(offset, examOrder);
	}

	@Override
	public Grade findGradeByExamineeId(int id) {
		return gradeDao.findGradeByExamineeId(id);
	}

	@Override
	public int findGradeByDeptIdAndZcj(int deptId, int examOrder) {
		return gradeDao.findGradeByDeptIdAndZcj(deptId, examOrder);
	}

	@Override
	public int findGradeByDeptId(int deptId, int examOrder) {
		return gradeDao.findGradeByDeptId(deptId, examOrder);
	}

	/*
	 * Rank对象的事务处理
	 */
	public void saveRank(Rank rank) {
		rankDao.save(rank);
	}

	public void deleteRankById(int id) {
		Rank rank = rankDao.findById(id);
		rankDao.delete(rank);
	}

	public void updateRank(Rank rank) {
		rankDao.update(rank);
	}

	public List<Rank> findRanks() {
		return rankDao.findAll();
	}

	public Rank findRankById(int id) {
		return rankDao.findById(id);
	}

	public List<Rank> findRanksOfEnabled() {
		return rankDao.findRanksOfEnabled();
	}

	@Override
	public Rank findRankByCode(String code) {
		return rankDao.findRankByCode(code);
	}

	/*
	 * Parameter对象的事务处理
	 */
	public void saveParameter(Parameter parameter) {
		parameterDao.save(parameter);
	}

	public void deleteParameterById(int id) {
		Parameter parameter = parameterDao.findById(id);
		parameterDao.delete(parameter);
	}

	public void updateParameter(Parameter parameter) {
		parameterDao.update(parameter);
	}

	public List<Parameter> findParameters() {
		return parameterDao.findAll();
	}

	public Parameter findParameterById(int id) {
		return parameterDao.findById(id);
	}

	public String getSchoolCode() {
		String schoolCode = "";
		List<Parameter> pList = findParameters();
		if (pList != null && pList.size() > 0) {
			schoolCode = pList.get(0).getSchoolCode();
		}
		return schoolCode;
	}

	@Override
	public int getExamOrderOfNow() {
		int examOrder = 0;
		List<Parameter> pList = findParameters();
		if (pList != null && pList.size() > 0) {
			examOrder = pList.get(0).getExamOrder();
		}
		return examOrder;
	}

	@Override
	public Parameter getParameterOfNow() {
		Parameter param = new Parameter();
		List<Parameter> pList = findParameters();
		if (pList != null && pList.size() > 0) {
			param = pList.get(0);
		}
		return param;
	}

	@Override
	public Enroll findEnrollByIdCardAndSerialNumber(String idCard,
			String serialNumber, int examOrder) {
		return enrollDao.findEnrollByIdCardAndSerialNumber(idCard,
				serialNumber, examOrder);
	}

	@Override
	public String getMaxENRecord(String rankCode) {
		// 获取当前最大报名号
		MaxSNRecord max = maxSNDao.findById(1);
		int maxENInt = max.getEnrollNumber();
		String maxEN = String.valueOf(maxENInt);
		StringBuffer prefix = new StringBuffer();
		for (int i = 1; i <= 6 - maxEN.length(); i++) {
			prefix.append("0");
		}

		// 更新当前最大报名号，对其加1
		max.setEnrollNumber(maxENInt + 1);
		maxSNDao.update(max);

		return getSchoolCode() + rankCode + prefix + maxEN;
	}

	@Override
	public synchronized String getMaxSNRecord() {
		// 获取当前最大网报号
		MaxSNRecord max = maxSNDao.findById(1);
		int maxSNInt = max.getSerialNumber();
		String maxSN = String.valueOf(maxSNInt);
		StringBuffer prefix = new StringBuffer();
		for (int i = 1; i <= 6 - maxSN.length(); i++) {
			prefix.append("0");
		}

		// 更新当前最大网报号，对其加1
		max.setSerialNumber(maxSNInt + 1);
		maxSNDao.update(max);

		return getSchoolCode() + prefix + maxSN;
	}

	@Override
	public void clearMaxSNRecord() {
		// 将最大网报号和最大报名号置为1
		MaxSNRecord max = maxSNDao.findById(1);
		if (max == null) {
			max = new MaxSNRecord();
			max.setEnrollNumber(1);
			max.setSerialNumber(1);
			maxSNDao.save(max);
		} else {
			max.setSerialNumber(1);
			max.setEnrollNumber(1);
			maxSNDao.update(max);
		}
	}

}
